/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package config;

import java.util.Collections;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import share.PGHelper;

/**
 *
 * @author Salm
 */
public class CFMiniGame implements JSONable {

    private CFMiniGame() {}
    
    public static CFMiniGame parse(Map<String, Object> json)
    {
        CFMiniGame miniGame = new CFMiniGame();
        miniGame.deser(json);
        
        return miniGame;
    }
    
    private FreakingEggs freakingEggs;
    
    private int maxStock;
    private int price;
    private int limitBuyPerDay;
    
    @Override
    public void deser(Map<String, Object> json) {
        this.maxStock = PGHelper.toInt(json.get("maxStock"));
        this.price = PGHelper.toInt(json.get("price"));
        this.limitBuyPerDay = PGHelper.toInt(json.get("limitBuyPerDay"));
        
        Map<String, Object> feJSON = (Map) json.get("freaking_eggs");
        freakingEggs = new FreakingEggs();
        freakingEggs.deser(feJSON);
    }

    public int getMaxStock() {
        return maxStock;
    }

    public int getPrice() {
        return price;
    }

    public int getLimitBuyPerDay() {
        return limitBuyPerDay;
    }

    public FreakingEggs getFreakingEggs() {
        return freakingEggs;
    }
    
    public static class FreakingEggs implements JSONable
    {
        private FreakingEggs() {}
        
        public NavigableMap<Integer, Map<String, Object>> prizes;

        @Override
        public void deser(Map<String, Object> json) {
            prizes = new TreeMap();
            Map<String, Object> prizesJSON = (Map) json.get("prizes");
            
            for (Map.Entry<String, Object> prizeEntry : prizesJSON.entrySet()) {
                int turn = PGHelper.toInt(prizeEntry.getKey());
                Map<String, Object> prizeData = (Map) prizeEntry.getValue();
                Map<String, Object> prize = Collections
                        .unmodifiableMap((Map) prizeData.get("prize"));
                
                prizes.put(turn, prize);
            }
        }
        
        public Map<String, Object> getPrize(int turn)
        {
            Integer minTurn = prizes.floorKey(turn);
            if (minTurn != null)
            {
                return prizes.get(minTurn);
            }
            
            return Collections.EMPTY_MAP;
        }
    }
}
